package com.beamfield.controller.manage.mixstation;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.DeviceCheck;
import com.beamfield.model.DeviceDeman;
import com.beamfield.model.DeviceDemanDetail;
import com.beamfield.model.DeviceDemarcate;
import com.beamfield.model.DeviceExit;
import com.beamfield.service.manage.mixstation.MixDeviceService;

/**
 * 拌和站设备
 */
@Controller
@RequestMapping(value = "/cms/mixstation/device")
public class MixDeviceController extends BaseController {
    private final MixDeviceService mixDeviceService;

    @Autowired
    public MixDeviceController(MixDeviceService mixDeviceService) {
        this.mixDeviceService = mixDeviceService;
    }

    @RequestMapping(value = "/device", method = RequestMethod.GET)
    public ModelAndView device() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/jinchangjianyan", method = RequestMethod.GET)
    public ModelAndView jinchangjianyan() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/purchase_list", method = RequestMethod.GET)
    public ModelAndView purchase_list() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/richangshiyong", method = RequestMethod.GET)
    public ModelAndView richangshiyong() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/tuichangguanli", method = RequestMethod.GET)
    public ModelAndView tuichangguanli() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/demand_list", method = RequestMethod.GET)
    public ModelAndView demand_list() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/addDemand", method = RequestMethod.GET)
    public ModelAndView addDemand() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/demandDetails", method = RequestMethod.GET)
    public ModelAndView demandDetails() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/biaodingjilu", method = RequestMethod.GET)
    public ModelAndView biaodingjilu() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/jiaoyan", method = RequestMethod.GET)
    public ModelAndView jiaoyan() {
        return new ModelAndView();
    }

    @RequestMapping(value = "/checklist", method = RequestMethod.GET)
    public ModelAndView checklist() {
        return new ModelAndView();
    }

    /**
     * 保存需求计划
     *
     * @param demandDetailJson
     * @param demandName
     * @param projectId
     * @param type
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveDemand", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(String demandDetailJson,Long id, String demandName, String projectId, int type, Integer recordSource, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        long userId = super.getUserId(request);
        if (StringUtils.isEmpty(demandDetailJson)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        if (StringUtils.isEmpty(projectId)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        if (StringUtils.isEmpty(demandName)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage("请输入需求计划名称");
            return apiResult;
        }
        try {
            List<DeviceDemanDetail> demandDetailList = JSON.parseArray(demandDetailJson, DeviceDemanDetail.class);
            if (demandDetailList == null || demandDetailList.isEmpty()) {
                return apiResult;
            }
            if(recordSource == null || recordSource == 0){
                recordSource = 1;
            }
            if(id == null){
                apiResult = mixDeviceService.saveDemand(demandDetailList, demandName, Long.parseLong(projectId), type, beamId, userId, recordSource);
            }else {
                apiResult = mixDeviceService.updateDemand(demandDetailList,id,demandName,type,beamId,userId);
            }

        } catch (Exception e) {
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }

    @RequestMapping(value = "/saveDemarcate")
    @ResponseBody
    public ApiResult saveDemarcate(DeviceDemarcate deviceDemarcate, String demarcate_date2, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        long userId = super.getUserId(request);
        if (deviceDemarcate == null || StringUtils.isBlank(demarcate_date2)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        apiResult = mixDeviceService.saveDemarcate(deviceDemarcate, demarcate_date2, beamId, userId);
        return apiResult;
    }

    @RequestMapping(value = "/saveExit")
    @ResponseBody
    public ApiResult saveExit(DeviceExit deviceExit, String exit_date2, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        long userId = super.getUserId(request);
        if (deviceExit == null || StringUtils.isBlank(exit_date2)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        apiResult = mixDeviceService.saveExit(deviceExit, exit_date2, beamId, userId);
        return apiResult;
    }

    @RequestMapping(value = "/saveDeviceCheck")
    @ResponseBody
    public ApiResult saveDeviceCheck(String objectJson, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        long userId = super.getUserId(request);
        DeviceCheck deviceCheck = JSON.parseObject(objectJson, DeviceCheck.class);
        if (deviceCheck == null) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        apiResult = mixDeviceService.saveDeviceCheck(deviceCheck, beamId, userId);
        return apiResult;
    }

    @RequestMapping(value = "/readCheckList")
    @ResponseBody
    public PageResult readCheckList(Long inboundid, Long offset, int limit) {
        if (inboundid == null) {
            return null;
        }
        PageResult deviceCheck = mixDeviceService.getDeviceCheck(inboundid, offset, limit);
        return deviceCheck;
    }

    @RequestMapping(value = "/getExitList")
    @ResponseBody
    public PageResult getExitList(Long offset, int limit, String recordSource,HttpServletRequest request) {
        long beamId = super.getBeamId(request);
        return mixDeviceService.getExitList(offset, limit,beamId,recordSource);
    }

    /**
     * 需求计划列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readDemandData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult demandList(HttpServletRequest request) {
        String projectId = request.getParameter("projectId");
        String demandName = request.getParameter("demandName");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String recordSource = request.getParameter("recordSource");
        Long beamId = super.getBeamId(request);
        List<DeviceDeman> demandPlanList = mixDeviceService.getdemand(projectId, demandName, offset, limit, recordSource, beamId);
        long total = mixDeviceService.countdemand(projectId, demandName, recordSource, beamId);
        return new PageResult(total, Long.parseLong(offset), demandPlanList);
    }

    /**
     * 获取计划
     * @param request
     * @return
     * @author anruoxin
     * 2019年4月10日
     */
    @RequestMapping(value = "/getDemangList")
    @ResponseBody
    public ApiResult getDemangList(HttpServletRequest request, String recordSource) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        List<DeviceDeman> demandPlanList = mixDeviceService.getdemand(null, null, null, null, recordSource, beamId);
        if (demandPlanList != null) {
			apiResult = new ApiResult(demandPlanList);
		}else {
			apiResult = new ApiResult(new ArrayList<>());
		}
        return apiResult;
    }


    @RequestMapping(value = "/deleteDemand")
    @ResponseBody
    public ApiResult deleteDemand(Long id, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = super.getBeamId(request);
        long userId = super.getUserId(request);
        if (id == null) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        apiResult = mixDeviceService.deleteDemand(id);
        return apiResult;
    }

    @RequestMapping(value = "/getExit")
    @ResponseBody
    public ApiResult getExit(HttpServletRequest request) {
        String inboundid = request.getParameter("inboundid");
        return mixDeviceService.getExit(Long.parseLong(inboundid));
    }

    @RequestMapping(value = "/readDemarcate", method = RequestMethod.GET)
    @ResponseBody
    public PageResult readDemarcate(HttpServletRequest request) {
        String inboundid = request.getParameter("inboundid");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        List<DeviceDemarcate> demandPlanList = mixDeviceService.readDemarcate(inboundid, offset, limit);
        long total = mixDeviceService.countReadDemarcate(inboundid);
        return new PageResult(total, Long.parseLong(offset), demandPlanList);
    }

    @RequestMapping(value = "/getDeviceList", method = RequestMethod.GET)
    @ResponseBody
    public PageResult getDeviceList(HttpServletRequest request) {
        String offset = request.getParameter("offset");
        String manager_num = request.getParameter("manager_num");
        String device_name = request.getParameter("device_name");
        String limit = request.getParameter("limit");
        String unit = request.getParameter("unit");
        long beamId = super.getBeamId(request);
        PageResult demandPlanList = mixDeviceService.getDeviceList(offset, limit, beamId, manager_num, device_name, unit);
        return demandPlanList;
    }

    /**
     * 获取需求计划明细列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readDemandDetailsData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult demandDetailsList(HttpServletRequest request) {
        String demandId = request.getParameter("demandId");
        String projectId = request.getParameter("projectId");
        if (StringUtils.isEmpty(demandId) || StringUtils.isEmpty(projectId)) {
            return null;
        }
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String suppliesName = request.getParameter("deviceName");
        List<DeviceDemanDetail> demandDetailVoList = mixDeviceService.getdemandDetails(demandId, suppliesName, offset, limit, projectId);
        long total = mixDeviceService.countDetailsByDemandId(demandId, suppliesName, projectId);
        return new PageResult(total, Long.parseLong(offset), demandDetailVoList);
    }

    /**
     * 获取采购计划剩余可采购的设备数量
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readCanPurchaseNum", method = RequestMethod.GET)
    @ResponseBody
    public List<DeviceDemanDetail> readCanPurchaseNum(HttpServletRequest request) {
        String demandId = request.getParameter("demandId");
        String projectId = request.getParameter("projectId");
        if (StringUtils.isEmpty(demandId) || StringUtils.isEmpty(projectId)) {
            return null;
        }
        return mixDeviceService.readCanPurchaseNum(demandId);
    }

    /**
     * 获取需求计划详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getDemandDetails", method = RequestMethod.GET)
    @ResponseBody
    public List<DeviceDemanDetail> getDemandDetails(HttpServletRequest request) {
        String demandId = request.getParameter("demandId");
        if (StringUtils.isEmpty(demandId)) {
            return null;
        }
        return mixDeviceService.getdemandDetails(demandId, null, null, null, "1");
    }

}
